package com.gcph.zhuoba.player.activitys;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.VideoView;

import com.gcph.zhuoba.R;
import com.gcph.zhuoba.main.bean.Resource;
import com.gcph.zhuoba.main.common.BaseActivity;
import com.gcph.zhuoba.main.common.CommonVedioActivity;
import com.gcph.zhuoba.main.common.Constant;
import com.gcph.zhuoba.util.L;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
/**
 * 解答的详情页面
 * @author zhanghejie
 */
public class AnswerDetailActivity extends BaseActivity {
	private String resourceId;// 资源ID
	private String resourceDesc;// 资源描述
	private String parentResourceType;//父类教案类型 
	private VideoView videoView;
	private String videoFileUrl;
	private ImageButton back;
	private TextView parent_resource_type_text;
	
	private ProgressBar loading_video;
	private FrameLayout video_frame;
	private ImageView full_screen;
	
	private Button answer_detail_do;
	
	private Resource resource; 

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_answer_detail);
		Intent intent=getIntent();
		resourceId=intent.getStringExtra("resourceId");
		resourceDesc=intent.getStringExtra("resourceDesc");
		
		String videoUrl=intent.getStringExtra("videoFileUrl");
		
		L.d("---------------videoUrl-"+videoUrl);
				
		TextView descTextView = (TextView) findViewById(R.id.resource_detail_desc);
		descTextView.setText(resourceDesc);
		
		//初始化按钮，并设置监听
		viewInitAndSetClickListener();
		
	}
	
	@Override
	protected void onResume() {
		new GetResourceTask().execute();
		super.onResume();
	}
	
	/**
	 * 初始化按钮，并设置监听
	 */
	private void viewInitAndSetClickListener()
	{
		parent_resource_type_text = (TextView) findViewById(R.id.parent_resource_type_text);
		back = (ImageButton) findViewById(R.id.btn_back);
		videoView = (VideoView) findViewById(R.id.practice_detail_video_view);
		
		loading_video = (ProgressBar) this.findViewById(R.id.loading_video);
		video_frame = (FrameLayout) this.findViewById(R.id.video_frame);
		full_screen = (ImageView) this.findViewById(R.id.btn_fullscreen);
		
		//去模仿
		answer_detail_do = (Button) this.findViewById(R.id.answer_detail_do);
		
		// 设置返回按钮事件
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		//视频加载完成
		videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
			@Override
			public void onPrepared(MediaPlayer mp) {
				video_frame.removeView(loading_video);
				videoView.seekTo(1);
			}
		});
		
		//全屏
		full_screen.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(AnswerDetailActivity.this,CommonVedioActivity.class);
				intent.putExtra("videoFileUrl", videoFileUrl);
				startActivity(intent);
			}
		});
		
		//去模仿
		answer_detail_do.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(AnswerDetailActivity.this,PlayerImitateActivity.class);
				//把当前资源传过去
				Bundle bundel = new Bundle();
				bundel.putSerializable("resource", resource);
				intent.putExtras(bundel);
				startActivity(intent);
			}
		});
	}
	
	/**
	 * 异步读取教案信息
	 * @author zhanghejie
	 */
	private class GetResourceTask extends AsyncTask<Void, Void, Void> {
		@Override
		protected Void doInBackground(Void... params) {
			HttpUtils http = new HttpUtils();
			http.configCurrentHttpCacheExpiry(0l);//不缓存请求数据
			
			RequestParams httpParams=new RequestParams();
			httpParams.addBodyParameter("id", resourceId);
			http.send(HttpRequest.HttpMethod.POST, getResources().getString(R.string.api_resource_get),httpParams,
					new RequestCallBack<String>() {
						@Override
						public void onSuccess(ResponseInfo<String> responseInfo) {
							
							String response = responseInfo.result;
							L.d("GetResourceTask response=="+response);
							resource = new Gson().fromJson(response, Resource.class);
							if(null != resource)
							{
								
								parentResourceType =  Constant.getResourceText(Constant.RESOURCE_TYPE_QIUZHU,resource.getResourceSmallType());
								parent_resource_type_text.setText("晓晨的"+parentResourceType+">>");
								videoFileUrl=resource.getVideoFileUrl();
								
								if(null != videoFileUrl && !"".equals(videoFileUrl) && !videoFileUrl.contains("http"))
								{
//									videoView.setVideoURI(Uri.parse(getResources().getString(R.string.server_root_url) + videoFileUrl));
//									MediaController mediaco = new MediaController(AnswerDetailActivity.this);
//									videoView.setMediaController(mediaco);
//									mediaco.setMediaPlayer(videoView);
//									videoView.seekTo(1);
									
								
									downLoadVedio(AnswerDetailActivity.this,videoView,videoFileUrl);
									
								}
								
								// 点击查看所属求助的详情
								parent_resource_type_text.setOnClickListener(new OnClickListener() {
									@Override
									public void onClick(View v) {
										// 跳转到教案详情页
										Intent intent = new Intent(AnswerDetailActivity.this, OnlineHelpDetailActivity.class);
										intent.putExtra("resourceId", String.valueOf(resource.getParentId()));
										startActivity(intent);
									}
								});
							}
						}

						@Override
						public void onFailure(HttpException error, String msg) {
						}
					});
			
			return null;
		}
	}
}